//
//  BBPunchCardView.swift
//  BBetter
//
//  Created by Gmc on 2020/3/14.
//  Copyright © 2020 Gmc. All rights reserved.
//

import UIKit

class BBPunchCardView: UIView {
    private var parentView:UIView?
    private var containerView = UIView()
    private var cancel:UIButton = UIButton(type: .custom)
    private var save:UIButton = UIButton(type: .custom)
    private var todayLabel = UILabel()
    private var todayInput = UITextField()
    private var descLabel = UILabel()
    private var descInput = UITextView()
    open var callback:(([String: Any])->Void)?
    private var parameter:[String: Any] = [String: Any]()
    override init(frame: CGRect) {
        super.init(frame: frame)
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapAction(gesture:)))
        addGestureRecognizer(tap)
        backgroundColor = UIColor(white: 0.3, alpha: 0.7)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        
    }
    
    @objc private func tapAction(gesture: UITapGestureRecognizer) {
        endEditing(true)
    }
    
    @objc private func saveAction(sender: UIButton) {
        endEditing(true)
        if parameter["today"] == nil || Int("\(parameter["today"] ?? 0)") == 0 || "\(String(describing: parameter["text"]))".count == 0 {
            
        } else {
            callback?(parameter)
            hide()
        }
    }
    
    @objc private func cancelAction(sender: UIButton) {
        if let _ = parentView {
            hide()
        }
    }
    
    open func show(view: UIView) {
        self.parentView = view
        let parentView = view
        if parentView.subviews.contains(self) {
            removeFromSuperview()
        }
        parentView.addSubview(self)
        snp.makeConstraints { (make) in
            make.edges.equalTo(parentView)
        }
        initSubviews()
    }
    
    open func hide() {
        if let _ = parentView {
            if parentView!.contains(self) {
                removeFromSuperview()
            }
        }
    }
    
}

extension BBPunchCardView {
    private func initSubviews() {
        do {
            addSubview(containerView)
            if #available(iOS 13.0, *) {
                if traitCollection.userInterfaceStyle == .dark {
                    containerView.backgroundColor = .black
                } else {
                    containerView.backgroundColor = .white
                }
            } else {
                containerView.backgroundColor = .white
            }
            containerView.layer.cornerRadius = 10
            containerView.clipsToBounds = true
            let topMargin = UIScreen.main.bounds.size.height > 813 ? 188 : 164
            containerView.snp.makeConstraints { (make) in
                make.top.equalTo(self).offset(topMargin)
                make.trailing.equalTo(self).offset(-40)
                make.leading.equalTo(self).offset(40)
                make.height.equalTo(240)
            }
        }
        do {
            cancel.setTitle("取消", for: .normal)
            
            if (CheckDate.check(date: "04-04") || CheckDate.check(date: "12-13")) {
                cancel.setTitleColor(UIColor.colorWithHexString(hexString: "272727"), for: .normal)
            } else {
                cancel.setTitleColor(UIColor.colorWithHexString(hexString: "00BFFF"), for: .normal)
            }

            cancel.setTitleColor(UIColor.colorWithHexString(hexString: "DDDDDF"), for: .highlighted)
            cancel.addTarget(self, action: #selector(cancelAction(sender:)), for: .touchUpInside)
            containerView.addSubview(cancel)
            cancel.snp.makeConstraints { (make) in
                make.bottom.equalTo(containerView).offset(-6)
                make.leading.equalTo(containerView).offset(20)
            }
            save.setTitle("保存", for: .normal)
            
            if (CheckDate.check(date: "04-04") || CheckDate.check(date: "12-13")) {
                save.setTitleColor(UIColor.colorWithHexString(hexString: "272727"), for: .normal)
            } else {
                save.setTitleColor(UIColor.colorWithHexString(hexString: "00BFFF"), for: .normal)
            }

            save.setTitleColor(UIColor.colorWithHexString(hexString: "DDDDDF"), for: .highlighted)
            save.addTarget(self, action: #selector(saveAction(sender:)), for: .touchUpInside)
            containerView.addSubview(save)
            save.snp.makeConstraints { (make) in
                make.bottom.equalTo(containerView).offset(-6)
                make.trailing.equalTo(containerView).offset(-20)
            }
        }
        do {
            todayLabel.text = "今日进度: "
            todayInput.layer.borderColor = UIColor.colorWithHexString(hexString: "DDDDDF").cgColor
            todayInput.layer.borderWidth = 1.0
            todayInput.layer.cornerRadius = 4.0
            todayInput.delegate = self
            todayInput.setValue(NSNumber(value: 10), forKey: "paddingLeft")
            todayInput.font = UIFont.systemFont(ofSize: 15)
            todayInput.keyboardType = .numberPad
            containerView.addSubview(todayLabel)
            todayLabel.snp.makeConstraints { (make) in
                make.leading.equalTo(containerView).offset(20)
                make.top.equalTo(containerView).offset(20)
                make.width.equalTo(80)
            }
            containerView.addSubview(todayInput)
            todayInput.snp.makeConstraints { (make) in
                make.leading.equalTo(todayLabel.snp_trailing).offset(10)
                make.centerY.equalTo(todayLabel)
                make.height.equalTo(28)
                make.trailing.equalTo(containerView).offset(-20)
            }
        }
        do {
            descLabel.text = "今日感言: "
            descInput.layer.borderColor = UIColor.colorWithHexString(hexString: "DDDDDF").cgColor
            descInput.delegate = self
            descInput.textContainerInset = UIEdgeInsets(top: 5, left: 6, bottom: 0, right: 0)
            descInput.layer.borderWidth = 1.0
            descInput.layer.cornerRadius = 4.0
            descInput.font = UIFont.systemFont(ofSize: 15)
            containerView.addSubview(descLabel)
            descLabel.snp.makeConstraints { (make) in
                make.leading.equalTo(containerView).offset(20)
                make.top.equalTo(todayLabel.snp_bottom).offset(20)
                make.width.equalTo(80)
            }
            containerView.addSubview(descInput)
            descInput.snp.makeConstraints { (make) in
                make.leading.equalTo(descLabel.snp_trailing).offset(10)
                make.top.equalTo(descLabel)
                make.height.equalTo(120)
                make.trailing.equalTo(containerView).offset(-20)
            }
        }
    }
}

extension BBPunchCardView: UITextFieldDelegate, UITextViewDelegate {
    func textFieldDidEndEditing(_ textField: UITextField) {
        if let text = textField.text {
            parameter["today"] = Int(text)
        }
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        
        return true
    }
    func textFieldDidChangeSelection(_ textField: UITextField) {
        
    }
    
    func textViewDidEndEditing(_ textView: UITextView) {
        parameter["text"] = textView.text
    }
}


class BBDetailShowView: UIView {
    private var text:String = ""
    private var parentView:UIView?
    private var containerView = UIView()
    private var showView = UITextView()
    private var cancel:UIButton = UIButton(type: .custom)
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 0.3, alpha: 0.7)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func initSubviews() {
        do {
            addSubview(containerView)
            if #available(iOS 13.0, *) {
                if traitCollection.userInterfaceStyle == .dark {
                    containerView.backgroundColor = .black
                } else {
                    containerView.backgroundColor = .white
                }
            } else {
                containerView.backgroundColor = .white
            }
            containerView.layer.cornerRadius = 10
            containerView.clipsToBounds = true
            let topMargin = UIScreen.main.bounds.size.height > 813 ? 188 : 164
            containerView.snp.makeConstraints { (make) in
                make.top.equalTo(self).offset(topMargin)
                make.trailing.equalTo(self).offset(-40)
                make.leading.equalTo(self).offset(40)
                make.height.equalTo(350)
            }
        }
        do {
            cancel.setTitle("取消", for: .normal)
            
            if (CheckDate.check(date: "04-04") || CheckDate.check(date: "12-13")) {
                cancel.setTitleColor(UIColor.colorWithHexString(hexString: "272727"), for: .normal)
            } else {
                cancel.setTitleColor(UIColor.colorWithHexString(hexString: "00BFFF"), for: .normal)
            }

            cancel.setTitleColor(UIColor.colorWithHexString(hexString: "DDDDDF"), for: .highlighted)
            cancel.addTarget(self, action: #selector(cancelAction(sender:)), for: .touchUpInside)
            containerView.addSubview(cancel)
            cancel.snp.makeConstraints { (make) in
                make.bottom.equalTo(containerView).offset(-6)
                make.trailing.equalTo(containerView).offset(-20)
            }
        }
        do {
            showView.layer.borderColor = UIColor.colorWithHexString(hexString: "DDDDDF").cgColor
            showView.textContainerInset = UIEdgeInsets(top: 5, left: 6, bottom: 0, right: 0)
            showView.layer.borderWidth = 1.0
            showView.layer.cornerRadius = 4.0
            showView.text = text
            showView.isEditable = false
            showView.font = UIFont.systemFont(ofSize: 15)
            containerView.addSubview(showView)
            showView.snp.makeConstraints { (make) in
                make.leading.equalTo(containerView).offset(10)
                make.top.equalTo(containerView).offset(10)
                make.bottom.equalTo(cancel.snp_top).offset(-10)
                make.trailing.equalTo(containerView).offset(-20)
            }
        }
    }
    
    @objc private func cancelAction(sender: UIButton) {
        if let _ = parentView {
            hide()
        }
    }
    
    open func show(view: UIView, text: String) {
        self.parentView = view
        self.text = text
        let parentView = view
        if parentView.subviews.contains(self) {
            removeFromSuperview()
        }
        parentView.addSubview(self)
        snp.makeConstraints { (make) in
            make.edges.equalTo(parentView)
        }
        initSubviews()
    }
    
    open func hide() {
        if let _ = parentView {
            if parentView!.contains(self) {
                removeFromSuperview()
            }
        }
    }
}

